﻿var selected;

function pageReady() {
    selected = null;
}

function RowHighLight(obj) {

    // Return if this is the selected row
    if (selected == obj)
        return;

    // Return if this isn't selectable
    if (obj.lastChild.lastChild.nodeValue == "No")
        return;
                
    // Highlight row
    obj.style.backgroundColor = "#89B215";
}

function RowNormal(obj) {

    // Return if this is the selected row
    if (selected == obj)
        return;

    // Return if this isn't selectable
    if (obj.lastChild.lastChild.nodeValue == "No")
        return;

    // Change background to normal color
    obj.style.backgroundColor = "White";
}

function SelectRow(obj) {

    // Return if this isn't selectable
    if (obj.lastChild.lastChild.nodeValue == "No")
        return;

    // Turn current selection white ie. unselect previous selected row
    if (selected != "" && selected != null) {
        selected.lastChild.lastChild.nodeValue = "Yes";
        selected.style.backgroundColor = "White";
    }
    
     // Return if this is the same row already selected
    if (selected == obj) {
        selected.lastChild.lastChild.nodeValue = "Yes";
        selected = null;
        return;
    }

    // Highlight row
    obj.style.backgroundColor = "#89B215";
    selected = obj;
    submitTimeSlot();
}

function submitTimeSlot() {
    
    // Return if nothing is selected
    if (selected == null)
        return;

    // Grab the time slot
    var timeSlot = selected.firstChild.lastChild.nodeValue;

    // Update state
    selected.lastChild.lastChild.nodeValue = "Reserve";
}

function SubmitTime() {
    // to do :  Handle the case where there are no open times
    if (selected == null) {
        alert("Please select a time slot.");
        return;
    }

    var timeSlot = selected.firstChild.lastChild.nodeValue;
    var end = timeSlot.indexOf(":", 0);
    var time = timeSlot;
    if (end > 0)
        time = timeSlot.substring(0, end);
    var actionCall = "/Calendar/ReserveTime/" + time.toString();

    location.replace(actionCall);
}

function MobileSubmit(obj) {
    location.replace("/Calendar/ReserveTime/" + obj);
}

function Cancel() {
    location.replace("/Calendar/Index");
}

function Schedule() {
    location.replace("/Calendar/ShowSchedule");
}